import store, { type StateAll } from "@/store";
import _ from "lodash";
import {
  createRouter,
  createWebHistory,
  type RouteRecordRaw,
} from "vue-router";
const Login = () => import("@/views/home/Login/Login.vue");
const Home = () => import("@/views/home/Home/Home.vue");
const Sign = () => import("@/views/home/Sign/Sign.vue");
const Exception = () => import("@/views/home/Exception/Exception.vue");
const Apply = () => import("@/views/home/Apply/Apply.vue");
const Check = () => import("@/views/home/Check/Check.vue");
import layout from "../layout/layout.vue";
interface RouteMeta {
  menu?: boolean;
  title?: string;
  icon?: string;
  auth?: boolean;
}
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "home",
    component: Home,
    redirect: "/sign",
    meta: {
      menu: true,
      title: "考勤管理",
      icon: "document-copy",
      auth: true,
    },
    children: [
      {
        path: "sign",
        name: "sign",
        component: Sign,
        meta: {
          menu: true,
          title: "在线打卡签到",
          icon: "calendar",
          auth: true,
        },
        beforeEnter(to, from, next) {
          const usersInfos = (store.state as StateAll).users.infos;
          const signsInfos = (store.state as StateAll).signs.infos;
          if (_.isEmpty(signsInfos)) {
            store
              .dispatch("signs/getTime", { userid: usersInfos._id })
              .then((res) => {
                if (res.data.errcode === 0) {
                  store.commit("signs/updateInfos", res.data.infos);
                  next();
                }
              });
          } else {
            next();
          }
        },
      },
      {
        path: "exception",
        name: "exception",
        component: Exception,
        meta: {
          menu: true,
          title: "异常考勤查询",
          icon: "warning",
          auth: true,
        },
        beforeEnter(to, from, next) {
          const usersInfos = (store.state as StateAll).users.infos;
          const signsInfos = (store.state as StateAll).signs.infos;
          if (_.isEmpty(signsInfos)) {
            store
              .dispatch("signs/getTime", { userid: usersInfos._id })
              .then((res) => {
                if (res.data.errcode === 0) {
                  store.commit("signs/updateInfos", res.data.infos);
                  next();
                }
              });
          } else {
            next();
          }
        },
      },
      {
        path: "apply",
        name: "apply",
        component: Apply,
        meta: {
          menu: true,
          title: "添加考勤审批",
          icon: "document-add",
          auth: true,
        },
      },
      {
        path: "check",
        name: "check",
        component: Check,
        meta: {
          menu: true,
          title: "我的考勤审批",
          icon: "finished",
          auth: true,
        },
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  // {
  //   path: "/home",

  //   meta: { title: "vue+ts项目", icon: "HomeFilled", menuType: 1 },
  //   component: layout,
  //   children: [
  //     {
  //       path: "hoe",
  //       component: () => import("@/views/home/index.vue"),
  //       meta: { title: "社交按钮" },
  //     },
  //     {
  //       name: "login",
  //       path: "login",
  //       meta: { title: "登录", icon: "User", menuType: 2 },
  //       component: () => import("@/views/login.vue"),
  //     },
  //   ],
  // },
];
const router = createRouter({
  history: createWebHistory(),
  routes: routes,
});
//添加路由守卫，进行权限控制
router.beforeEach((to, from, next) => {
  const token = (store.state as StateAll).users.token;
  const infos = (store.state as StateAll).users.infos;
  // 这一段的逻辑：判断是否有token，基本信息是否为空，如果token是伪造的也不通过
  if (to.meta.auth && _.isEmpty(infos)) {
    if (token) {
      //有token然后请求得到infos信息
      store.dispatch("users/infos").then((res) => {
        if (res.data.errcode === 0) {
          //更新store
          store.commit("users/updateInfos", res.data.infos);
          next();
        }
      });
    } else {
      next("/login");
    }
  } else {
    if (token && to.path === "/login") {
      next("/");
    } else {
      next();
    }
  }
});
export default router;
